package com.controller;

import com.constant.Constant;
import com.entity.GoldPoints;
import com.entity.User;
import com.service.LoginDateService;
import com.util.AjaxResult;
import com.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.DateUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class LoginDateController {

    @Autowired
    LoginDateService updateLoginDateService;

    //判断是否需要签到
    @RequestMapping("/user/findLoginDate.do")
    public Boolean findLoginDate(HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        User attribute = (User)session.getAttribute(Constant.SESSION_USER);//后台放在session的user
        System.out.println(attribute+"获取上次签到的时间");
        Date date = updateLoginDateService.finLoginDateS(attribute);
        boolean flag = false;
        if(date == null ){
            flag =true;
        } else{
            flag = DateUtil.dateUtil(date);
        }
        System.out.println("是否需要签到"+flag);
        return  flag ;
    }

    //签到后更新时间
    @RequestMapping("/user/updateLoginDate.do")
    public AjaxResult updateLoginDate( User user){
        int user_id = user.getId();
        GoldPoints goldPoints1 = new GoldPoints();
        GoldPoints goldPoints = new GoldPoints(user_id ,5,0,"签到获得5积分",new Date());
        int i1 = updateLoginDateService.UpdateLoginDateByIdS(goldPoints);
        if (i1 == 1){
            return AjaxResult.isOk("签到成功");
        }else {
            return AjaxResult.isFail("签到失败");
        }
    }

    //展示个人积分数
    @RequestMapping("/gp/findFrontSumByUid")
    public AjaxResult findFrontSumByUidC(HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        GoldPoints frontSumByUidS1 = updateLoginDateService.findFrontSumByUidS(session);
        ajaxResult.setObj(frontSumByUidS1);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
